//
//  THJProductDetailVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/4.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJProductDetailVC.h"
#import "THJDataBannerCell.h"
#import "THJProductTitleCell.h"
#import "THJProductDetailCell.h"
#import "THJProductCompanyCell.h"
#import "THJCompanyVC.h"
#import "THJCommitOrderView.h"

@interface THJProductDetailVC ()<UITableViewDelegate,UITableViewDataSource,THJProductCompanyCellDelegate>
{
    UITableView * _tableview;
    NSInteger successFlag;
}
@property(nonatomic,strong)NSDictionary * infoDic;
@end

@implementation THJProductDetailVC

- (void)viewDidLoad{
    [super viewDidLoad];
    [self.phoneBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
    [self.storeBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
    [self setTableview];
    [self setGoBackBtn];
    [self getData];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStateBarColor:[UIColor clearColor]];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setHidden:YES];
}
-(void)getData{
    [THJNetWork GetProductInfoWithProductItemId:self.itemid Callback:^(NSDictionary* receiveData, NSError *error) {
        if([receiveData[@"status"] isEqual:@"200"]){
            successFlag = 1;
            self.infoDic = receiveData[@"data"];
            NSLog(@"_*_%@",self.infoDic);
            [_tableview reloadData];
        }else{
            [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:2];
            successFlag =0;
             [_tableview reloadData];
        }
        
    }];
}
-(void)setTableview{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, ScreenHeight - 61) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJDataBannerCell" bundle:nil] forCellReuseIdentifier:@"banner"];
    [_tableview registerNib:[UINib nibWithNibName:@"THJProductTitleCell" bundle:nil] forCellReuseIdentifier:@"title"];
    [_tableview registerNib:[UINib nibWithNibName:@"THJProductDetailCell" bundle:nil] forCellReuseIdentifier:@"detail"];
    [_tableview registerNib:[UINib nibWithNibName:@"THJProductCompanyCell" bundle:nil] forCellReuseIdentifier:@"company"];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 200;
    }else if(indexPath.row == 1){
        return 140;
    }else if (indexPath.row == 2){
        return 300;
    }else{
        return 180;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        THJDataBannerCell * cell = [tableView dequeueReusableCellWithIdentifier:@"banner" forIndexPath:indexPath];
        NSMutableArray * imgArr = [[NSMutableArray alloc]init];
        NSString * str =  self.infoDic[@"thumb"];
        if(str.length){
            [imgArr addObject:self.infoDic[@"thumb"]];
            NSString * str1 =  self.infoDic[@"thumb1"];
            if(str1.length){
                [imgArr addObject:self.infoDic[@"thumb1"]];
                NSString * str2 =  self.infoDic[@"thumb2"];
                if(str2.length){
                    [imgArr addObject:self.infoDic[@"thumb2"]];
                }
            }
            [cell setBannerWithImgUrlArr:imgArr Height:200];
        }else{
            [cell setBannerWithImgUrlArr:nil Height:200];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.row == 1){
        THJProductTitleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"title" forIndexPath:indexPath];
        if(successFlag == 1){
            cell.titleLab.text = self.infoDic[@"title"];
            if([self.infoDic[@"price"] isEqual:@"0.00"] || [self.infoDic[@"price"] isEqual:[NSNull null]]){
                cell.priceLab.text =  @"价格电议";
            }else{
                cell.priceLab.text =  [NSString stringWithFormat:@"￥%@",self.infoDic[@"price"]];
            }
            
            cell.jhdLab.text =  [NSString stringWithFormat:@"产地:%@",self.infoDic[@"jhd"]];
            cell.kucunLab.text = [NSString stringWithFormat:@"库存:%@吨",self.infoDic[@"amount"]];
        }else{
            cell.titleLab.text = @"";
            cell.priceLab.text = @"";
            cell.jhdLab.text = @"";
            cell.kucunLab.text = @"";
            cell.tunLab.text = @"";
        }
       
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else if(indexPath.row == 2){
        THJProductDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setCellContentWithInfoDic:self.infoDic];
        return cell;
    }else{
        THJProductCompanyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"company" forIndexPath:indexPath];
        [cell setContentWithDic:self.infoDic];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setShadowView:cell.productView];
        return cell;
    }
}

/**
 分享
 */
-(void)shareBtnAction{
    NSString * shareUrl = [NSString stringWithFormat:@"http://bigdata.thjb.net/dist/#/store/phoneprodetail/%@",self.infoDic[@"itemid"]];
   
    [self shareWithContent:self.infoDic[@"title"] Title:self.infoDic[@"title"] Url:shareUrl];
}
/**
 关于
 */
-(void)getCompany{
    THJCompanyVC * vc= [THJCompanyVC new];
    vc.cid = self.infoDic[@"cid"];
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 联系
 
 */
- (IBAction)phontBtnClick:(id)sender {
    NSString * str;
    if(!self.infoDic[@"tel"] || [self.infoDic[@"tel"] isEqual:[NSNull null]]){
        str  = [NSString stringWithFormat:@"telprompt://%@",self.infoDic[@"tel"]];
    }else{
        str = @"telprompt://400-999-6014";
    }
   
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

/**
 商铺

 @param sender btn
 */
- (IBAction)storeBtnClick:(id)sender {
}

/**
 立即订购

 @param sender btn
 */
- (IBAction)buyBtnClick:(id)sender {
    THJCommitOrderView * view = [THJCommitOrderView instancView];
    view.backgroundColor = COLOR(0, 0, 0, 0.5);
    view.itemid = self.infoDic[@"itemid"];
    view.catname = self.infoDic[@"catname"];
    view.infoDic = self.infoDic;
    NSLog(@"jj-%@",self.infoDic);
    view.frame = self.view.frame;
    [self.view addSubview:view];
}

/**
 设置阴影

 @param view view
 */
-(void)setShadowView:(UIView*)view{
    //设置阴影
    view.layer.shadowColor = [UIColor grayColor].CGColor;//设置阴影的颜色
    view.layer.shadowOpacity = 0.8;//设置阴影的透明度
    view.layer.shadowOffset = CGSizeMake(3, 3);//设置阴影的偏移量
    view.layer.shadowRadius = 3;//设置阴影的圆角
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
}
@end
